const Db = require('./db')
const vk = require('./vk')
class article
{
  static async list(ctx){
    let params = ctx.query
    let pageStart = (params.page - 1)*params.limit
    let class_id = params.class_id?params.class_id:'',
        title = params.title?params.title:'';
    let data = {
      sql:"SELECT a.id,a.pic,a.title,a.sort,a.author,a.class_id,a.hits,a.createTime,b.title AS product_title FROM vk_article_list a LEFT JOIN vk_product_list b ON a.product_id = b.id WHERE a.class_id = CASE WHEN ? != '' THEN ? ELSE a.class_id END AND a.title LIKE '%"+ title +"%' ORDER BY a.sort DESC LIMIT "+pageStart+","+params.limit,
      values:[class_id,class_id]
    }
    let [err,res] = await Db.query(data)
    console.log(res,class_id)
    if(err){return ctx.body = vk.showData(500,err,"请求失败")}
    let countSql = {
      sql:"SELECT COUNT(id) FROM vk_article_list",
    }
    let [err2,count] = await Db.query(countSql)
    if(err2){return ctx.body = vk.showData(500,err2,"请求失败")}
    let o = {
      data:res,
      count:count[0]['COUNT(id)']
    }
    ctx.body = vk.showData(200,o,'ok')
  }

  static async detail(ctx){
    let params = ctx.query
    let data = {
      sql:"SELECT * FROM vk_article_list WHERE id = ?",
      values:[params.id]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,'请求失败')}
    ctx.body = vk.showData(200,res,'ok')
  }

  static async edit(ctx){
    let params = ctx.request.body
    let data = {
      sql:"UPDATE vk_article_list SET title=?,pic=?,author=?,content=?,hits=?,class_id=?,sort=?,product_id=? WHERE id = ?",
      values:[params.title,params.pic,params.author,params.content,params.hits,params.class_id,params.sort,params.product_id,params.id]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,'请求失败')}
    ctx.body = vk.showData(200,[],'添加成功')
  }

  static async add(ctx){
    let params = ctx.request.body
    let product_id = params.product_id?params.product_id:0
    let data = {
      sql:"INSERT INTO vk_article_list (title,pic,author,content,hits,class_id,product_id,createTime) VALUES (?,?,?,?,?,?,?,?)",
      values:[params.title,params.pic,params.author,params.content,params.hits,params.class_id,product_id,Math.floor((new Date()).valueOf()/1000)]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,'请求失败')}
    ctx.body = vk.showData(200,[],'添加成功')
  }

  static async del(ctx){
    let params = ctx.request.body
    let data = {
      sql:"DELETE FROM vk_article_list WHERE id = ?",
      values:[params.id]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,'请求失败')}
    ctx.body = vk.showData(200,[],'添加成功')
  }

  static async classList(ctx){
    let params = ctx.query
    let id = params.id?params.id:''
    let keyword = params.keyword?params.keyword:''
    let data = {
      sql:"SELECT * FROM vk_article_classify WHERE id = CASE WHEN ? != '' THEN ? ELSE id END AND title LIKE '%"+keyword.replace(/\s/g,"")+"%'",
      values:[id,id]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,'请求错误')}
    ctx.body = vk.showData(200,res,'ok')
  }

  static async classAdd(ctx){
    let params = ctx.request.body
    let data = {
      sql:"INSERT INTO vk_article_classify (title,sort,pid,pic,is_show) VALUES (?,?,?,?,?)",
      values:[params.title,params.sort,params.pid,params.pic,params.is_show]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,'请求错误')}
    ctx.body = vk.showData(200,res,'ok')
  }

  static async classEdit(ctx){
    let params = ctx.request.body
    let data = {
      sql:"UPDATE vk_article_classify SET title = ?,sort = ?,pid = ?,pic = ?,is_show = ? WHERE id = ?",
      values:[params.title,params.sort,params.pid,params.pic,params.is_show,params.id]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,'请求错误')}
    ctx.body = vk.showData(200,res,'ok')
  }

  static async classDel(ctx){
    let params = ctx.request.body
    let data = {
      sql:"DELETE FROM vk_article_classify WHERE id = ?",
      values:[params.id]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,'请求错误')}
    ctx.body = vk.showData(200,res,'ok')
  }

  // 前端接口
  static async getList(ctx){
    let data = {
      sql:"(SELECT a.id AS class_id,a.title AS class_title,a.pid,a.pic AS class_pic,b.id AS article_id,b.title AS article_title FROM vk_article_classify a RIGHT JOIN vk_article_list b ON a.id = b.class_id AND a.is_show = 1) UNION (SELECT a.id AS class_id,a.title AS class_title,a.pid,a.pic AS class_pic,b.id AS article_id,b.title AS article_title FROM vk_article_classify a LEFT JOIN vk_article_list b ON a.id = b.class_id AND a.is_show = 1 ORDER BY a.sort DESC)"
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,'请求错误')}
    ctx.body = vk.showData(200,res,'ok')
  }

  static async getDetail(ctx){
    let params = ctx.query
    let data = {
      sql:"SELECT title,author,content,hits,createTime FROM vk_article_list WHERE id = ?",
      values:[params.id]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,'请求错误')}
    let hits = res[0].hits
    let addHits = {
      sql:"UPDATE vk_article_list SET hits = "+parseInt(hits + 1)+" WHERE id = ?",
      values:[params.id]
    }
    let [err2,res2] = await Db.query(addHits)
    if(err2){return ctx.body = vk.showData(500,err,'请求错误')}
    res[0].hits = parseInt(res[0].hits + 1)
    ctx.body = vk.showData(200,res,'ok')
  }
}

module.exports = article